#include "util.h"
#include "buffer.h"
#include "httprequest.h"
#include "httpresponse.h"
#include "httpserver.h"
#include "httpcontext.h"
#include <iostream>
#include <vector>
#include <memory>

void TestSplit()
{
    std::string src = "ab,cd,,,,,ef,df,";
    std::string sep = ",";
    std::vector<std::string> des;
    Util::split(src, sep, &des);
    std::cout << src << std::endl;
    for(auto& s : des)
    {
        std::cout << "[" << s << "]" << std::endl;
    }
}
void TestReadFile()
{
    Buffer buf;
    bool ret = Util::readFile("wwwroot/test.txt", &buf);
    if(ret == true)
    {
        std::cout << buf.getReadAddr() << std::endl;
    }
}
void TestWriteFile()
{
    std::string src = "sfw is good\n";
    bool ret = Util::writeFile("wwwroot/test.txt", src);

}
void TestUrlEncode()
{
    std::string url = "/wwwroot/index.html?language=c++     ";
    auto ret = Util::urlEncode(url);
    std::cout << ret << std::endl;
}
void TestUrlDecode()
{
    std::string url = "%2Fwwwroot%2Findex.html%3Flanguage%3Dc%2B%2B++++++";
    auto ret = Util::urlDecode(url);
    std::cout << "[" << ret << "]" << std::endl;
}
void TestStatuDes()
{
    int m = 404;
    std::cout << Util::statuDes(m) << std::endl;
}
void TestMimeDes()
{
    std::string filename = "text.txt";
    std::cout << Util::mimeDes(filename) << std::endl;
}
void TestIsDirectory()
{
    std::string filename = "../echo/echo.cc";
    std::cout << Util::isDirectory(filename) << std::endl;
}
void TestIsRegular()
{
    std::string filename = "../echo/echo.cc";
    std::cout << Util::isRegular(filename) << std::endl;
}
void TestValidPath()
{

}
void TestHttpRequest()
{
    std::string request_line = "GET /a/b?name=sfw&passwd=66666 HTTP/1.1\r\n";
    std::string request_head = "Host: 82.157.154.92:8888\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36\r\nConnection: close\r\nContent-Length: 12\r\nCookie: b-user-id=8627f400-cde9-bc1b-e834-95b8fe8c3008\r\nUpgrade-Insecure-Requests: 1\r\nX-Forwarded-For: 1.1.1.1\r\n";
    Buffer buf;
    buf.writeString(request_line + request_head);
}
void Hello(const HttpRequest& req, HttpResponse* rsp)
{
    rsp->setBody("server say# hello", "text/plain");
}
void TestHttpServer()
{
    std::unique_ptr<HttpServer> server(new HttpServer(58888, 30, "0.0.0.0"));
    server->setBaseDic("wwwroot");
    server->setThreadCount(2);
    server->get("/Hello", Hello);
    server->start();
}
int main()
{
    //TestSplit();
    // TestReadFile();
    // TestWriteFile();
    // TestReadFile();
    //TestUrlEncode();
    //TestUrlDecode();
    // TestStatuDes();
    // TestMimeDes();
    // TestIsDirectory();
    // TestIsRegular();
    // TestHttpRequest();
    TestHttpServer();
    return 0;
}